/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/**
 * 广告日志列表
 */
import React, { useMemo, useRef, useCallback } from 'react';
import ProTable from '@ant-design/pro-table';
import DragTable from '@/components/DragTable';
import { Button } from 'antd';
import { getAdvLogList } from '@/services/operatorManagement';
import { useDebounceFn } from '@ant-design/pro-utils';
import type { ProColumns, ActionType } from '@ant-design/pro-table';
import type { AdvLogType } from '../types/Advertisements';

const AdvLog: React.FC = () => {
  const formRef = useRef<ActionType>();

  const columns: ProColumns<AdvLogType>[] = useMemo(
    () => [
      {
        title: '操作时间',
        width: 200,
        dataIndex: 'created_at',
        align: 'center',
        search: false,
      },
      {
        title: '操作人',
        width: 150,
        dataIndex: 'operator',
        align: 'center',
        search: false,
      },
      {
        title: '操作内容',
        width: 150,
        dataIndex: 'action',
        align: 'center',
        search: false,
      },
      {
        title: '广告标题',
        width: 150,
        dataIndex: 'ad_name',
        align: 'center',
        search: false,
      },
    ],
    [],
  );

  const getTableList = useCallback((params) => {
    const { current: page, pageSize, ...rest } = params;
    return getAdvLogList({ page, pageSize, ...rest });
  }, []);

  const onRefresh = useDebounceFn(async () => {
    formRef.current?.reload();
  }, 300);

  return (
    <>
      <DragTable>
        <ProTable<AdvLogType>
          className='kb-pro-table'
          actionRef={formRef}
          rowKey='id'
          columns={columns}
          options={{
            fullScreen: false,
            reload: false,
            setting: false,
            density: false,
          }}
          toolBarRender={() => [
            <Button type='primary' onClick={onRefresh.run}>
              刷新
            </Button>,
          ]}
          search={false}
          request={getTableList}
          pagination={{
            pageSize: 20,
            showSizeChanger: false,
          }}
          scroll={{ x: 500 }}
        />
      </DragTable>
    </>
  );
};

export default AdvLog;
